package com.hns.can.safety.web;

import java.net.URLEncoder;
import java.util.Arrays;
import java.util.List;

import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.SessionAttributes;

import com.hns.can.base.entity.SysFieldValueRegisterEntity;
import com.hns.can.base.entity.SysUserEntity;
import com.hns.can.common.domain.GridPage;
import com.hns.can.common.entity.BaseEntity;
import com.hns.can.common.service.CommonInitParamService;
import com.hns.can.common.web.BaseController;
import com.hns.can.master.entity.CommDrvSctBhvInfoEntity;
import com.hns.can.safety.service.DrivingBehaviorService;
import com.hns.can.safety.vo.DrivingBehaviorParamVo;
import com.hns.frame.enumrate.MsgType;
import com.hns.frame.exception.BusinessException;
import com.hns.frame.mybatis.Pagination;
import com.hns.tool.export.ExportUtil;
import com.hns.tool.pub.DateTool;
import com.hns.tool.pub.PubUtil;

/**
 * 驾驶安全行为Controller
 * @author:zhangzq
 * @email:
 * @version Revision 2.0.0
 */
@Controller
@RequestMapping("/drivingBehavior")
public class DrivingBehaviorController extends BaseController{
	
	//用于导出驾驶员安全行为详情
    final private String[] headerNames1={"驾驶员", "车牌号", "车辆编号", "营运线路","所属机构", "异常行为","持续时长","车速(km/h)","转速(r/min)","发生时间","发生站点"};
    final private String[] headerFields1 = new String[]{"drvName", "licPltNo", "carInCd", "lineName", "organName", "drvSctBhv", "duration", "reservedField1", "reservedField2","sctBhvBeginTime","happenlocation"};
    //用于导出驾驶安全行为统计,线路维度
    final private String[] headerNames2={"排名","营运线路","所属机构","异常行为总数","车辆未停稳开车门","车辆起步不关车门","超车","急转弯","空档滑行","倒档时车速超速报警","ABS制动（急刹）","夜间行驶过长","起步急加速","熄火滑行","急加速","急减速","急刹","不文明鸣笛","斑马线不文明礼让","超速"};
    final private String[] headerFields2 = new String[]{ "ranking","lineName","organName","totalbhvcount","bhv1count", "bhv2count", "bhv3count", "bhv4count","bhv5count", "bhv6count", "bhv7count", "bhv8count", "bhv9count","bhv10count", "bhv11count", "bhv12count", "bhv13count","bhv14count","bhv15count","spedCnt"};
    //用于导出驾驶安全行为统计，车辆维度
    final private String[] headerNames3={"排名","车辆","营运线路","线路均值","异常行为总数","车辆未停稳开车门","车辆起步不关车门","超车","急转弯","空档滑行","倒档时车速超速报警","ABS制动（急刹）","夜间行驶过长","起步急加速","熄火滑行","急加速","急减速","急刹","不文明鸣笛","斑马线不文明礼让","超速"};
    final private String[] headerFields3 = new String[]{"ranking","licPltNo","lineName","lineAverage","totalbhvcount", "bhv1count", "bhv2count", "bhv3count", "bhv4count","bhv5count", "bhv6count", "bhv7count", "bhv8count", "bhv9count","bhv10count", "bhv11count", "bhv12count", "bhv13count","bhv14count","bhv15count","spedCnt"};
	//用于导出驾驶安全行为统计，驾驶员维度
	final private String[] headerNames4={"排名","驾驶员","营运线路","线路均值","异常行为总数","车辆未停稳开车门","车辆起步不关车门","超车","急转弯","空档滑行","倒档时车速超速报警","ABS制动（急刹）","夜间行驶过长","起步急加速","熄火滑行","急加速","急减速","急刹","不文明鸣笛","斑马线不文明礼让","超速"};
	final private String[] headerFields4 = new String[]{"ranking","drvName","lineName","lineAverage","totalbhvcount", "bhv1count", "bhv2count", "bhv3count", "bhv4count","bhv5count", "bhv6count", "bhv7count", "bhv8count", "bhv9count","bhv10count", "bhv11count", "bhv12count", "bhv13count","bhv14count","bhv15count","spedCnt"};

	@Autowired
	private DrivingBehaviorService drivingBehaviorService;
	@Autowired
    private CommonInitParamService commonInitParamService;
	
	/**
	 * 检验参数是否为空
	 * @param conditionVo
	 * @return
	 * @author:zhangzq
	 * @email:
	 */
	private boolean checkParams(DrivingBehaviorParamVo conditionVo){
		if (PubUtil.isEmpty(conditionVo.getType())|| PubUtil.isEmpty(conditionVo.getOrgIds())|| PubUtil.isEmpty(conditionVo.getBeginTime())|| PubUtil.isEmpty(conditionVo.getEndTime())) {
			return false;
		}
		return true;
	}
	
	/**
	 * 参数初始化
	 * @param conditionVo
	 * @author:zhangzq
	 * @email:
	 */
	private void initParams(DrivingBehaviorParamVo conditionVo){
		SysUserEntity user = this.getCurrentUser().getUser();
		conditionVo.setCorpId(user.getCorpId());
		conditionVo.setDriverRelTableName("T_COMM_DRV_SCT_BHV_INFO");
	}
	
	/**
	 * 分页查询驾驶员行为数据
	 * @param conditionVo
	 * @return
	 * @throws Exception
	 * @author:zhangzq
	 * @email:
	 */
	@RequestMapping("/searchDrivingBehavior")
	@ResponseBody
	public Object searchDrivingBehavior(DrivingBehaviorParamVo conditionVo)throws Exception {
		if (!checkParams(conditionVo)){
			return null;
		}
		initParams(conditionVo);
		Pagination page = drivingBehaviorService.searchDrivingBehavior(conditionVo);
		GridPage grid = new GridPage(page);
		return this.getReturnResult(grid);
	}
	
	/**
	 * 导出驾驶员安全行为Excel文档
	 * @param conditionVo
	 * @param response
	 * @throws Exception
	 * @author:zhangzq
	 * @email:
	 */
	@RequestMapping("/exportDrivingBehavior2Excel")
    public void exportDrivingBehavior2Excel(DrivingBehaviorParamVo conditionVo, HttpServletResponse response) throws Exception {
		if (!checkParams(conditionVo)){
			return;
		}
		initParams(conditionVo);
		List<BaseEntity> list = drivingBehaviorService.getDrivingBehaviorList(conditionVo);
        if(PubUtil.isEmpty(list)){
            throw new BusinessException(MsgType.WARN.getFlag(), "没有数据导出！");
        }
        ExportUtil.exportFiles(Arrays.asList(headerNames1), Arrays.asList(headerFields1), list, response, "excel", URLEncoder.encode("驾驶员不安全行为详情", "UTF-8")+DateTool.getCurrentSysDate("yyyyMMddHHmmss"), "驾驶员安全行为数据");
    }
	
	/**
	 * 导出驾驶员安全行为Pdf文档
	 * @param conditionVo
	 * @param response
	 * @throws Exception
	 * @author:zhangzq
	 * @email:
	 */
	@RequestMapping("/exportDrivingBehavior2Pdf")
    public void exportDrivingBehavior2Pdf(DrivingBehaviorParamVo conditionVo, HttpServletResponse response) throws Exception {
		if (!checkParams(conditionVo)){
			return;
		}
		initParams(conditionVo);
		List<BaseEntity> list = drivingBehaviorService.getDrivingBehaviorList(conditionVo);
        if(PubUtil.isEmpty(list)){
            throw new BusinessException(MsgType.WARN.getFlag(), "没有数据导出！");
        }
        ExportUtil.exportFiles(Arrays.asList(headerNames1), Arrays.asList(headerFields1), list, response, "pdf", URLEncoder.encode("驾驶员不安全行为详情", "UTF-8")+DateTool.getCurrentSysDate("yyyyMMddHHmmss"), "驾驶员安全行为数据");
    }
	
	/**
	 * 分页查询驾驶行为统计
	 * @param conditionVo
	 * @return
	 * @throws Exception
	 * @author:zhangzq
	 * @email:
	 */
	@RequestMapping("/searchDrivingBehaviorStatistics")
	@ResponseBody
	public Object searchDrivingBehaviorStatistics(DrivingBehaviorParamVo conditionVo)throws Exception {
		if (!checkParams(conditionVo)){
			return null;
		}
		initParams(conditionVo);
        Pagination page = drivingBehaviorService.searchDrivingBehaviorStatistics(conditionVo);
        GridPage grid = new GridPage(page);
        return this.getReturnResult(grid);
	}

    /**
     * 驾驶行为统计用于图表
     * @param conditionVo
     * @throws Exception
     * @author:zhangzq
     * @email:
     */
    @RequestMapping("/searchDrivingBehaviorStatisticsForChart")
    @ResponseBody
    public Object getDrivingBehaviorStatisticsForChart(DrivingBehaviorParamVo conditionVo)throws Exception {
        if (!checkParams(conditionVo)){
            return null;
        }
        initParams(conditionVo);
        List<CommDrvSctBhvInfoEntity> list = drivingBehaviorService.getDrivingBehaviorStatisticsForChart(conditionVo);
        return this.getReturnResult(list);
    }
	
	/**
	 * 导出驾驶行为统计Excel文档
	 * @param conditionVo
	 * @param response
	 * @throws Exception
	 * @author:zhangzq
	 * @email:
	 */
	@RequestMapping("/exportDrivingBehaviorStatistics2Excel")
	public void exportDrivingBehaviorStatistics2Excel(DrivingBehaviorParamVo conditionVo,HttpServletResponse response) throws Exception {
		if (!checkParams(conditionVo)){
			return ;
		}
		initParams(conditionVo);
		List<CommDrvSctBhvInfoEntity> list = drivingBehaviorService.getDrivingBehaviorStatistics(conditionVo);
       if(PubUtil.isEmpty(list)){
           throw new BusinessException(MsgType.WARN.getFlag(), "没有数据导出！");
       }
       if(conditionVo.getStatisType().equals("LINE")){
    	   ExportUtil.exportFiles(Arrays.asList(headerNames2), Arrays.asList(headerFields2), list, response, "excel", URLEncoder.encode("驾驶员不安全行为统计", "UTF-8")+DateTool.getCurrentSysDate("yyyyMMddHHmmss"), "驾驶安全行为统计数据");
       }
       else if(conditionVo.getStatisType().equals("CAR")){
    	   ExportUtil.exportFiles(Arrays.asList(headerNames3), Arrays.asList(headerFields3), list, response, "excel", URLEncoder.encode("驾驶员不安全行为统计", "UTF-8")+DateTool.getCurrentSysDate("yyyyMMddHHmmss"), "驾驶安全行为统计数据");
       }
	   else{
		   ExportUtil.exportFiles(Arrays.asList(headerNames4), Arrays.asList(headerFields4), list, response, "excel", URLEncoder.encode("驾驶员不安全行为统计", "UTF-8")+DateTool.getCurrentSysDate("yyyyMMddHHmmss"), "驾驶安全行为统计数据");
	   }
	}
	
	/**
	 * 导出驾驶行为统计Pdf文档
	 * @param conditionVo
	 * @param response
	 * @throws Exception
	 * @author:zhangzq
	 * @email:
	 */
	@RequestMapping("/exportDrivingBehaviorStatistics2Pdf")
	public void exportDrivingBehaviorStatistics2Pdf(DrivingBehaviorParamVo conditionVo, HttpServletResponse response) throws Exception {
		if (!checkParams(conditionVo)) {
			return;
		}
		initParams(conditionVo);
		List<CommDrvSctBhvInfoEntity> list = drivingBehaviorService.getDrivingBehaviorStatistics(conditionVo);
		if (PubUtil.isEmpty(list)) {
			throw new BusinessException(MsgType.WARN.getFlag(), "没有数据导出！");
		}
		if (conditionVo.getStatisType().equals("LINE")) {
			ExportUtil.exportFiles(Arrays.asList(headerNames2),Arrays.asList(headerFields2), list, response, "pdf",URLEncoder.encode("驾驶员不安全行为统计", "UTF-8") + DateTool.getCurrentSysDate("yyyyMMddHHmmss"),"驾驶安全行为统计数据");
		} else if (conditionVo.getStatisType().equals("CAR")) {
			ExportUtil.exportFiles(Arrays.asList(headerNames3),Arrays.asList(headerFields3), list, response, "pdf",URLEncoder.encode("驾驶员不安全行为统计", "UTF-8") + DateTool.getCurrentSysDate("yyyyMMddHHmmss"),"驾驶安全行为统计数据");
		} else{
			ExportUtil.exportFiles(Arrays.asList(headerNames4),Arrays.asList(headerFields4), list, response, "pdf",URLEncoder.encode("驾驶员不安全行为统计", "UTF-8") + DateTool.getCurrentSysDate("yyyyMMddHHmmss"),"驾驶安全行为统计数据");
		}
	}
	
	@RequestMapping("/getBhvDsc")
	@ResponseBody
	public Object getBhvDsc(){
		List<SysFieldValueRegisterEntity> list = drivingBehaviorService.getBhvDsc();
		return this.getReturnResult(list);
	}
}
